package logger

import (
	"fmt"
	"os"
	"strings"
	"time"
)

func Debug(tpl string, args ...interface{}) {
	printf("DEBUG", tpl, args...)
}

func Info(tpl string, args ...interface{}) {
	printf("INFO ", tpl, args...)
}

func Warn(tpl string, args ...interface{}) {
	printf("WARN ", tpl, args...)
}

func Error(tpl string, args ...interface{}) {
	printf("ERROR", tpl, args...)
}

func Fatal(tpl string, args ...interface{}) {
	printf("FATAL", tpl, args...)
	os.Exit(1)
}

func Panic(tpl string, args ...interface{}) {
	str := toString("PANIC", tpl, args...)
	panic(str)
}

func toString(level, tpl string, args ...interface{}) string {
	now := time.Now()
	sb := strings.Builder{}
	sb.WriteString(now.Format("2006-01-02 15:04:05"))
	sb.WriteString(fmt.Sprintf(" [%s] ", level))
	sb.WriteString(fmt.Sprintf(tpl, args...))
	return sb.String()
}

func printf(level, tpl string, args ...interface{}) {
	str := toString(level, tpl, args...)
	fmt.Println(str)
}
